package com.cruise.game.service.impl.admin;

import com.cruise.game.common.constants.CacheConstants;
import com.cruise.game.common.constants.OrderConstants;
import com.cruise.game.common.enums.UserStatusEnum;
import com.cruise.game.common.utils.RedisUtils;
import com.cruise.game.mapper.AgentMapper;
import com.cruise.game.mapper.OrderMapper;
import com.cruise.game.mapper.TransactionMapper;
import com.cruise.game.mapper.UserMapper;
import com.cruise.game.model.vo.admin.AdminStatisticsVO;
import com.cruise.game.model.vo.admin.FinanceStatisticsVO;
import com.cruise.game.service.admin.AdminStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 管理员统计服务实现
 * 
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Service
public class AdminStatisticsServiceImpl implements AdminStatisticsService {

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private UserMapper userMapper;

    @Resource
    private AgentMapper agentMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private TransactionMapper transactionMapper;

    @Override
    public AdminStatisticsVO getStatistics() {
        // 从缓存中获取统计数据
        AdminStatisticsVO statisticsVO = (AdminStatisticsVO) redisUtils.get(CacheConstants.STATISTICS_CACHE_PREFIX + "admin");
        if (statisticsVO != null) {
            return statisticsVO;
        }
        
        // 缓存中没有，从数据库中获取
        statisticsVO = new AdminStatisticsVO();
        
        // 总用户数
        statisticsVO.setTotalUsers(userMapper.countUsers(null, UserStatusEnum.ACTIVE.getValue()));
        
        // 在线用户数（暂时使用一个固定值）
        statisticsVO.setOnlineUsers(100);
        
        // 总代理数
        statisticsVO.setTotalAgents(agentMapper.countAgents(null));
        
        // 在线代理数（暂时使用一个固定值）
        statisticsVO.setOnlineAgents(20);
        
        // 待接单数
        statisticsVO.setPendingOrders(orderMapper.countByStatus(OrderConstants.ORDER_STATUS_PENDING));
        
        // 进行中订单数
        statisticsVO.setInProgressOrders(orderMapper.countByStatus(OrderConstants.ORDER_STATUS_IN_PROGRESS));
        
        // 已完成订单数
        statisticsVO.setCompletedOrders(orderMapper.countByStatus(OrderConstants.ORDER_STATUS_COMPLETED));
        
        // 售后中订单数
        statisticsVO.setAfterSalesOrders(orderMapper.countByStatus(OrderConstants.ORDER_STATUS_AFTER_SALES));
        
        // 已取消订单数
        statisticsVO.setCancelledOrders(orderMapper.countByStatus(OrderConstants.ORDER_STATUS_CANCELLED));
        
        // 缓存统计数据
        redisUtils.set(CacheConstants.STATISTICS_CACHE_PREFIX + "admin", statisticsVO, CacheConstants.STATISTICS_CACHE_EXPIRE);
        
        return statisticsVO;
    }

    @Override
    public FinanceStatisticsVO getFinanceStatistics(LocalDate startDate, LocalDate endDate) {
        // 创建财务统计对象
        FinanceStatisticsVO statisticsVO = new FinanceStatisticsVO();
        
        // 总充值金额
        BigDecimal totalRecharge = transactionMapper.sumRechargeAmount(startDate, endDate);
        statisticsVO.setTotalRecharge(totalRecharge != null ? totalRecharge : BigDecimal.ZERO);
        
        // 总提现金额
        BigDecimal totalWithdrawal = transactionMapper.sumWithdrawalAmount(startDate, endDate);
        statisticsVO.setTotalWithdrawal(totalWithdrawal != null ? totalWithdrawal : BigDecimal.ZERO);
        
        // 总订单金额
        BigDecimal totalOrderAmount = orderMapper.sumOrderAmount(startDate, endDate);
        statisticsVO.setTotalOrderAmount(totalOrderAmount != null ? totalOrderAmount : BigDecimal.ZERO);
        
        // 平台收入（这里简单假设平台收入为总订单金额的10%）
        BigDecimal platformIncome = totalOrderAmount != null ? totalOrderAmount.multiply(new BigDecimal("0.1")) : BigDecimal.ZERO;
        statisticsVO.setPlatformIncome(platformIncome);
        
        // 每日统计
        List<FinanceStatisticsVO.DailyStats> dailyStats = new ArrayList<>();
        // TODO: 实现每日统计
        statisticsVO.setDailyStats(dailyStats);
        
        return statisticsVO;
    }
}
